﻿Imports System.Data.OleDb
Public Class Detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'CHECK IF USER HAS ALREADY LIKED
        'Image Visibility
        img_Like.Visible = False

        ''SQL Instruction
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
        Dim sql = "SELECT * FROM UserLikes WHERE Author = @f1 and ProjectLike_ID = @f2"
        Dim cmd As New OleDbCommand(sql, conn)

        conn.Open()

        'Value Substitution
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))

        ''value Reader
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        ''Check Presence rows for matched Values to determine visibility 
        If dr.HasRows Then
            img_Like.Visible = True
            btn_Like.Visible = False
        Else
            img_Like.Visible = False
            btn_Like.Visible = True
        End If

        conn.Close()

    End Sub

    Protected Sub btn_AddComment_Click(sender As Object, e As EventArgs) Handles btn_AddComment.Click
        'POST COMMMENT
        'SQL Instruction
        Dim sql As String = "INSERT INTO Comments (Author,Title,CommentBody,Project_fkID) VALUES (@f1,@f2,@f3,@f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        'Value Substitutiion
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", txtbox_Title.Text)
        cmd.Parameters.AddWithValue("@f3", txtbox_Comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))

        'Establishing Connection
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub btn_Rate_Click(sender As Object, e As EventArgs) Handles btn_Rate.Click

        'DELETE RATING IF ALREADY EXIST/ADD RATING
        'SQL Instruction
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
        Dim delPrevRating As String = "DELETE FROM Rating WHERE Rater=@f1 AND Project_ID=@f2"
        Dim cmd As OleDbCommand = New OleDbCommand(delPrevRating, conn)

        'value Substition
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()

        'SQL Instruction
        Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
        Dim sql As String = "INSERT INTO Rating (Rating,Rater,Project_ID) VALUES (@f1,@f2,@f3)"
        Dim cmd2 As OleDbCommand = New OleDbCommand(sql, conn2)

        'Value Substitution
        cmd2.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd2.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd2.Parameters.AddWithValue("@f3", Request.Params("ID"))

        'Establish Connection
        conn2.Open()
        cmd2.ExecuteNonQuery()
        conn2.Close()
        cmd2.Dispose()
        conn2.Dispose()
        Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))
    End Sub

    Protected Sub btn_Like_Click(sender As Object, e As EventArgs) Handles btn_Like.Click

        'ADD LIKE FOR NEW USER
        'VIsibility change on Click
        btn_Like.Visible = False
        img_Like.Visible = True

        'SQL Instruction
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
        Dim sql As String = "INSERT INTO UserLikes (Author,ProjectLike_ID) VALUES (@f1,@f2)"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        'Value Substitution
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))

        'Connection
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()


    End Sub

    Protected Sub btn_catagory_Click(sender As Object, e As EventArgs) Handles btn_catagory.Click
        'ADDING CATAGORIES TO THE PROJECTS
        Dim sql As String = "INSERT INTO Link_ProjectCatagories (Project_fkID, Catagories_fkID) VALUES (@f1, @f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ArduinoPlaygroundConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()

        cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@f2", ddl_catagories.SelectedValue)
        cmd.ExecuteNonQuery()

    End Sub
End Class